package project.teg.games.game.pinch;

import project.teg.R;
import project.teg.common.types.ScreenType;
import project.teg.configuration.Global;
import project.teg.games.game.gestures.InteractiveObject;
import android.app.Activity;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Chronometer;
import android.widget.EditText;

public class GameActivity extends Activity implements OnTouchListener {

	GameView gameView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.pinchmain);
		Chronometer c = (Chronometer) findViewById(R.id.Chronometer01);
		c.start();

		EditText e = (EditText) findViewById(R.id.EditText01);
		e.setEnabled(false);
		getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

		gameView = (GameView) findViewById(R.id.PinchGameView);
		gameView.setOnTouchListener(this);
	}

	int br = 1;

	String ids = "";
	float initialDistance = 200;
	int id1 = -1, id2 = -1;
	boolean pinchStart = false;

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		if (gameView.currentScreen == ScreenType.WelcomeScreen) {
			
			gameView.currentScreen = ScreenType.NextLevel;
			gameView.invalidate();
			return false;
		} else if (gameView.currentScreen == ScreenType.NextLevel) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				if (event.getPointerCount() == 1) {
					id1 = event.getPointerId(event.getPointerCount() - 1);

				} else if (event.getPointerCount() == 2) {

					Log("--------------------------------------------");
					initialDistance = calculateDistance(
							new PointF(event.getX(0), event.getY(0)),
							new PointF(event.getX(1), event.getY(1)));
					id2 = event.getPointerId(event.getPointerCount() - 1);
					// Log("*********************initialDistance = " +
					// initialDistance);
				} else {
					Log("broj pointera: " + event.getPointerCount() + "");
				}
				Log(event.getPointerCount() + "");
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				if (event.getPointerCount() == 2) {
					if (!pinchStart) {
						pinchStart = true;
						PointF firstPoint = new PointF(event.getX(0),
								event.getY(0));
						PointF secondPoint = new PointF(event.getX(1),
								event.getY(1));
						initialDistance = calculateDistance(firstPoint,
								secondPoint);
					} else {
						PointF p1 = new PointF(event.getX(0), event.getY(0));
						PointF p2 = new PointF(event.getX(1), event.getY(1));
						float currentDistance = calculateDistance(p1, p2);
						float ratio = currentDistance / initialDistance;
						ratio = ratio > 0 ? ratio * 1.5f : ratio / 1.5f;
						gameView.interactiveObject.currentSize.x = gameView.interactiveObject.initSize.x
								* ratio;
						gameView.interactiveObject.currentSize.y = gameView.interactiveObject.initSize.y
								* ratio ;
					}
				}

			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				pinchStart = false;
				br++;
				gameView.interactiveObject = null;

				if (br > Global.maxLevels) {
					gameView.currentScreen = ScreenType.GameOverScreen;
				}
			}
			gameView.invalidate();
			return true;

		} else { // if (gameView.currentScreen == ScreenType.GameOverScreen) {

			if (event.getAction() == MotionEvent.ACTION_DOWN)
				this.finish();
			return true;
		}
	}

	private float calculateDistance(PointF a, PointF b) {
		return (float) Math.sqrt(Math.pow(a.x - b.x, 2)
				+ Math.pow(a.y - b.y, 2));
	}

	public void Log(PointF x, PointF y) {
		Log.e("Log", "(" + x.x + ", " + x.y + "),	(" + y.x + ", " + y.y + ")");
	}

	public void Log(PointF x) {
		Log.e("Log", x.x + ", " + x.y);
	}

	public void Log(String s) {
		Log.e("Log", s);
	}
}